package org.csu.onlineedubackend.controller;

import lombok.extern.slf4j.Slf4j;
import org.csu.onlineedubackend.entity.CourseSubscribe;
import org.csu.onlineedubackend.mapper.CourseSubscribeMapper;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.result.ResponseCode;
import org.csu.onlineedubackend.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {

    @Autowired
    private StudentCourseService studentCourseService;
    
    @Autowired
    private CourseSubscribeMapper courseSubscribeMapper;

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
    
    @GetMapping("/subscribe")
    public CommonResponse<?> testSubscribe(
            @RequestParam Integer courseId,
            @RequestParam Integer studentId,
            @RequestParam(defaultValue = "基础") String level,
            @RequestParam(defaultValue = "true") boolean isFree) {
        
        log.info("测试订阅 - 课程ID: {}, 学生ID: {}, 级别: {}, 是否免费: {}", courseId, studentId, level, isFree);
        
        try {
            boolean result = studentCourseService.subscribe(studentId, courseId, level, isFree);
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "测试订阅结果: " + result, result);
        } catch (Exception e) {
            log.error("测试订阅异常 - 课程ID: {}, 学生ID: {}, 错误: {}", courseId, studentId, e.getMessage(), e);
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "测试订阅异常: " + e.getMessage());
        }
    }
    
    @GetMapping("/unsubscribe")
    public CommonResponse<?> testUnsubscribe(
            @RequestParam Integer courseId,
            @RequestParam Integer studentId) {
        
        log.info("测试取消订阅 - 课程ID: {}, 学生ID: {}", courseId, studentId);
        
        try {
            boolean result = studentCourseService.unsubscribe(studentId, courseId);
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "测试取消订阅结果: " + result, result);
        } catch (Exception e) {
            log.error("测试取消订阅异常 - 课程ID: {}, 学生ID: {}, 错误: {}", courseId, studentId, e.getMessage(), e);
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "测试取消订阅异常: " + e.getMessage());
        }
    }
    
    @GetMapping("/direct-subscribe")
    public CommonResponse<?> testDirectSubscribe(
            @RequestParam Integer courseId,
            @RequestParam Integer studentId) {
        
        log.info("测试直接添加订阅记录 - 课程ID: {}, 学生ID: {}", courseId, studentId);
        
        try {
            CourseSubscribe subscribe = new CourseSubscribe();
            subscribe.setCourseId(courseId);
            subscribe.setStudentId(studentId);
            subscribe.setCreatedAt(LocalDateTime.now());
            
            int result = courseSubscribeMapper.insert(subscribe);
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "直接添加订阅记录结果: " + (result > 0), result > 0);
        } catch (Exception e) {
            log.error("直接添加订阅记录异常 - 课程ID: {}, 学生ID: {}, 错误: {}", courseId, studentId, e.getMessage(), e);
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "直接添加订阅记录异常: " + e.getMessage());
        }
    }
} 